package com.hs.communication.mqtt.producer;

import com.hs.communication.common.producer.IProducer;
import com.hs.communication.common.utils.ByteUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.hs.communication.mqtt.config.MqttConfig.MQTT_EXCHANGE_NAME;
import static com.hs.communication.mqtt.config.MqttConfig.MQTT_REPLY_NAME;

@Slf4j
@Component
public class Producer implements IProducer {
    @Resource
    private RabbitTemplate rabbitTemplate;

    public boolean send(String sn, byte[] message) {
        try {
            //指定消息 id 为 1
//        CorrelationData correlationData1 = new CorrelationData("1");
            CorrelationData correlationData1 = null;
            rabbitTemplate.convertAndSend(MQTT_EXCHANGE_NAME, MQTT_REPLY_NAME + "." + sn, message, correlationData1);
            log.info("发送消息内容:{}", ByteUtil.bytesToHex(message));
        } catch (Exception e) {
            System.out.println("发生异常");
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
